<%@ page language="java" contentType="text/html;charset=UTF-8" %><%@ include file="../../inc/global.jsp" 
%><compress:html enabled="true" removeComments="false" compressJavaScript="true" yuiJsDisableOptimizations="true">
<!doctype html>
<html>
<head>
<title>${adminGlobalConfig.name}</title>
<%@include file="../head.jsp" %>
<link type="text/css" rel="stylesheet" href="/static/choicegoods.css" media="all" />
<style type="text/css">
.form table tr th {width:150px;}
</style>
<script type="text/javascript">
function Dispatch() {
	var _this = this;
	function showTip(o, content) {
		$(o).poshytip({
			content: content,
			showOn: 'none',
			showTimeout: '5000',
			className: 'tip-yellow',
			alignTo: 'target',
			alignX: 'top',
			alignY: 'bottom',
			offsetX: -160,
			offsetY: 5
		}).poshytip('show');
	};
	function hideTip(o) {
		aju.removeAll([$('.tip-yellow')]);
	};
	this.init = function() {
		$('#button').click(function(){
			var fromId = $('#fromId').val();
			var storehouseId = $('#storehouseId').val();
			var goodsId = $('#goodsId').val();
			if(goodsId==undefined || goodsId.length==0) {
				showTip($('#goodsKey'), '请选择商品型号');
				return ;
			}
			else if(fromId==undefined || fromId.length==0) {
				showTip($('#fromList'), '请选择商品所在库房');
				return ;
			}
			else if(!storehouseId || storehouseId.length==0) {
				showTip($('#storehouseList'), '选择调度到目标库房');
				return ;
			}
			$(this).attr('disabled', 'true');
			var num = $('#num').val(); // 要调度的数量
			var url = '/a/store/dispatch/save';
			$.ajax({url:url
				, type:'POST'
				, data:{goodsId:goodsId
					, fromId:fromId
					, storehouseId:storehouseId
					, num:num
					}
				, success:function(data){
					var json = $.parseJSON(data);
					dispatchCount();
					showTip($('#button'), '商品调度成功！');
					$('#button').removeAttr('disabled');
				}, error:function(){
					alert('系统错误，请联系管理员!');
				}
			});
		});
		$('#num').keyup(function(){
			var content='';
			//alert($.isNumeric($(this).val()));
			var max = $('#tip strong').html();
			if(!$.isNumeric($(this).val())) {
				$(this).val('');
				content = '请输入数字';
			} else {
				var num = parseInt($(this).val());
				if(num>max) {
					content = '本库房最多可调度商品数量 ' + max + ' 件';
					$('#num').val(max);
				}
			}
			if(content.length>0) {
				hideTip($('#num'));
				$('#num').poshytip({
					content: content,
					showTimeout: '5000',
					className: 'tip-yellow',
					alignTo: 'target',
					alignX: 'center',
					alignY: 'bottom',
					offsetX: 0,
					offsetY: 5
				}).poshytip('show');
			}
		});
		//商品搜索开始
		defaultKeys('goodsKey').focus().dblclick(function(){
			choiceGoods.showGoods(function(id){
				_this.getGoodsInfo(id);
			});
		}).keydown(function(event){ // 搜索并显示结果列表
			// 如果没有关键词则显示商品筛选列表
			if(event.keyCode == KEY.ENTER) {
				choiceGoods.showGoods(function(id){
					_this.getGoodsInfo(id);
				});
			}
		});
		$(document).keydown(function(){
			if(event.keyCode == KEY.ESC) {
				choiceGoods.hide();
			}
		});
		$('.storehouseList').find('em').each(function(){
			$(this).click(function(){
				var storehouseId = $(this).attr('title');
				if(storehouseId==undefined) {
					storehouseId='0';
				}
				$(this).parent().find('em').removeClass('slt');
				var shid;
				if($(this).parent().attr('id')=='fromList') {
					hideTip($('#formList'));
					shid = $('#fromId').val();
					if(shid==undefined) {
						shid='';
					}
					if(shid.length>0 && shid==storehouseId) {
						$(this).removeClass('slt');
						$('#fromId').val('');
					} else {
						$(this).addClass('slt');
						$('#fromId').val(storehouseId);
						$('#storehouseList').find('em').each(function() {
							if($(this).attr('title') == $('#fromId').val()) {
								$(this).hide();
							} else {
								$(this).show();
							}
						});
					}
					dispatchCount();
				} else {
					hideTip($('#storeList'));
					shid = $('#storehouseId').val();
					if(shid==storehouseId) {
						$(this).removeClass('slt');
						$('#storehouseId').val('');
					} else {
						$(this).addClass('slt');
						$('#storehouseId').val(storehouseId);
					}
				}
			});
		});
	};
	// 统计某库房内存储未销售的商品数量
	function dispatchCount() {
		var shid = $('#fromId').val();
		var goodsId = $('#goodsId').val();
		if(shid==undefined) {
			shid='';
		}
		var url = "/a/store/dispatch/count?shid="+shid+"&gid="+goodsId;
		$.ajax({
			url:url
			, type:'GET'
			, success:function(data){
				var json = $.parseJSON(data);
				$('#tip').html('所选库房可调度商品数量 <strong class="red">'+json.data+'</strong>  件');
				$('#num').val(json.data);
			}
			, error:function(){
				$('#tip').html('所选库房可调度商品数量 <strong class="red">0</strong> 件');
				$('#num').val(0);
			}
		});
	};
	this.getGoodsInfo = function(id) {
		if(id!=undefined && id.length>0) {
			$('#goodsInfo').html(id);
			var url='/a/ajax/goods/'+id+'?r='+new Date();
			$.ajax({
				type:'GET'
				, url: url
				, success: function(data){
					var json = $.parseJSON(data).data;
					$("#brandId").val(json.brandId);
					$("#categoryId").val(json.categoryId);
					$("#id").val(json.id);
					$('#goodsId').val(json.id);
					$("#goodsInfo").html(
							json.brand.name
							+" "
							+json.category.name
							+" "
							+json.name
							+ " <span class='green'>("
							+json.displayName+")</span>");
					choiceGoods.hide();
				}
				, error: function(){
					choiceGoods.hide();
				}
			});
		}
	};
}
var dispatch = new Dispatch();
$(function(){
	dispatch.init();
});
</script> 
</head>
<body>
<div class="wrapper">
<%@include file="../header.jsp" %>
<div class="body">
<div class="form">
<table>
<tr>
<th>搜索</th>
<td><input type="text" id="goodsKey" name="goodsKey" value="输入商品名称或型号后回车" /></td>
</tr>
<tr>
<th><fmt:message key="message.goods.name" /></th>
<td>
<div id="goodsInfo">
<c:if test="${!empty goods}">
${brand.name } ${category.name} ${goods.name} <span class="green">(${goods.displayName})</span>
</c:if>
</div>
<input type="hidden" id="goodsId" name="goodsId" value="${goods.id}" />
</td>
</tr>
<tr>
<th>选择商品所在库房</th>
<td id="fromList" class="storehouseList">
<input type="hidden" id="fromId" name="fromId"/>
<em title="0"><span class="green">未入库</span></em>
<c:forEach var="item" items="${storehouseList}">
<em title="${item.id}">${item.name}</em>
</c:forEach>
</td>
</tr>
<tr>
<th>选择调度到目标库房</th>
<td id="storehouseList" class="storehouseList">
<input type="hidden" id="storehouseId" name="storehouseId"/>
<c:forEach var="item" items="${storehouseList}">
<em title="${item.id}">${item.name}</em>
</c:forEach>
</td>
</tr>
<tr>
<th>调度商品数量</th>
<td id="storehouseList">
<input type="text" id="num" name="num" value="" />
<br/>
<span id="tip" class="green"></span>
</td>
</tr>
<tr>
<th></th>
<td><input type="button" name="button" id="button" value='<fmt:message key="message.save.sure" />' /></td>
</tr>
</table>
</div>
<div class="sidebar">
</div>
</div>
</div>
</body>
</html>
</compress:html>